package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.HospitalSubscribeScheduleDaysVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HospitalSubscribeScheduleDaysMapper;
import com.ruoyi.system.domain.HospitalSubscribeScheduleDays;
import com.ruoyi.system.service.IHospitalSubscribeScheduleDaysService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 开放预约日Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-28
 */
@Service
public class HospitalSubscribeScheduleDaysServiceImpl implements IHospitalSubscribeScheduleDaysService 
{
    @Autowired
    private HospitalSubscribeScheduleDaysMapper hospitalSubscribeScheduleDaysMapper;

    /**
     * 查询开放预约日
     * 
     * @param id 开放预约日主键
     * @return 开放预约日
     */
    @Override
    public HospitalSubscribeScheduleDays selectHospitalSubscribeScheduleDaysById(Long id)
    {
        return hospitalSubscribeScheduleDaysMapper.selectHospitalSubscribeScheduleDaysById(id);
    }

    /**
     * 查询开放预约日列表
     * 
     * @param hospitalSubscribeScheduleDays 开放预约日
     * @return 开放预约日
     */
    @Override
    public List<HospitalSubscribeScheduleDays> selectHospitalSubscribeScheduleDaysList(HospitalSubscribeScheduleDays hospitalSubscribeScheduleDays)
    {
        return hospitalSubscribeScheduleDaysMapper.selectHospitalSubscribeScheduleDaysList(hospitalSubscribeScheduleDays);
    }

    /**
     * 新增开放预约日
     * 
     * @param hospitalSubscribeScheduleDays 开放预约日
     * @return 结果
     */
    @Override
    public int insertHospitalSubscribeScheduleDays(HospitalSubscribeScheduleDays hospitalSubscribeScheduleDays)
    {
        hospitalSubscribeScheduleDays.setCreateTime(DateUtils.getNowDate());
        return hospitalSubscribeScheduleDaysMapper.insertHospitalSubscribeScheduleDays(hospitalSubscribeScheduleDays);
    }

    /**
     * 修改开放预约日
     * 
     * @param hospitalSubscribeScheduleDays 开放预约日
     * @return 结果
     */
    @Override
    public int updateHospitalSubscribeScheduleDays(HospitalSubscribeScheduleDays hospitalSubscribeScheduleDays)
    {
        hospitalSubscribeScheduleDays.setUpdateTime(DateUtils.getNowDate());
        return hospitalSubscribeScheduleDaysMapper.updateHospitalSubscribeScheduleDays(hospitalSubscribeScheduleDays);
    }

    /**
     * 批量删除开放预约日
     * 
     * @param ids 需要删除的开放预约日主键
     * @return 结果
     */
    @Override
    public int deleteHospitalSubscribeScheduleDaysByIds(Long[] ids)
    {
        return hospitalSubscribeScheduleDaysMapper.deleteHospitalSubscribeScheduleDaysByIds(ids);
    }

    /**
     * 删除开放预约日信息
     * 
     * @param id 开放预约日主键
     * @return 结果
     */
    @Override
    public int deleteHospitalSubscribeScheduleDaysById(Long id)
    {
        return hospitalSubscribeScheduleDaysMapper.deleteHospitalSubscribeScheduleDaysById(id);
    }

    /**
     * 每天生成一天日期,并清理前一天的日期
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void saveDateTimeAndDeleteTheDayTime() {
        // 先查询出时间段模板
        int number = hospitalSubscribeScheduleDaysMapper.getTemplateNumber();
        hospitalSubscribeScheduleDaysMapper.saveDateTimeAndDeleteTheDayTime(number);
    }

    /**
     * 查询开放预约日列表
     * @return
     */
    @Override
    public List<HospitalSubscribeScheduleDaysVo> getHospitalSubscribeScheduleDaysList() {
        return hospitalSubscribeScheduleDaysMapper.getHospitalSubscribeScheduleDaysList()
                .stream().peek(item -> {
                    List<HospitalSubscribeScheduleDaysVo.HospitalSubscribeScheduleTimeVo> timeVoList = item.getTimeVoList();
                    int sum = timeVoList.stream().mapToInt(HospitalSubscribeScheduleDaysVo.HospitalSubscribeScheduleTimeVo::getAvailableSlots).sum();
                    item.setIsReducible(sum == 0 ? 1 : 0);
                }).collect(Collectors.toList());
    }
}
